<?php
declare (strict_types = 1);

namespace app\admin\model\user;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class UserCar extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        $info = $this->where('id',$id)->where('is_delete',1)->find();
        if($info){
            $userModel = new User();
            $user = $userModel->getInfoById($info['user_id']);
            $info['nickname'] = $user['nickname'];
            $info['account'] = $user['account'];
            $carTypeName = [0=>'未设置',1=>'纯电车',2=>'混合动力'];
            $authenticationName = [0=>'未验证',1=>'已验证'];
            $info['type_name'] = $carTypeName[$info['car_type']];
            $info['authentication_name'] = $authenticationName[$info['is_authentication']];
        }
        return $info;
    }

    /**
     * 获取列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit){
        $sql = $this;
        if(isset($getData['id']) && $getData['id']) $sql = $sql->where('id',$getData['id']);
        if(isset($getData['car_type']) && $getData['car_type']) $sql = $sql->where('car_type',$getData['car_type']);
        if(isset($getData['is_authentication']) && ($getData['is_authentication'] || $getData['is_authentication'] === 0 )) $sql = $sql->where('is_authentication',$getData['is_authentication']);
        if(isset($getData['use_type']) && $getData['use_type']) $sql = $sql->where('use_type',$getData['use_type']);
        if(isset($getData['car_brand']) && $getData['car_brand']) $sql = $sql->whereLike('car_brand','%'.$getData['car_brand'].'%');
        if(isset($getData['car_number']) && $getData['car_number']) $sql = $sql->whereLike('car_number','%'.$getData['car_number'].'%');
        if(isset($getData['vin_number']) && $getData['vin_number']) $sql = $sql->whereLike('vin_number','%'.$getData['vin_number'].'%');
        $count = $sql->count();
        $userModel = new User();
        $carTypeName = [0=>'未设置',1=>'纯电车',2=>'混合动力'];
        $authenticationName = [0=>'未验证',1=>'已验证'];
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($userModel,$carTypeName,$authenticationName){
            $userInfo = $userModel->getInfoById($item['user_id']);
            $item['nickname'] = $userInfo['nickname'];
            $item['account'] = $userInfo['account'];
            $item['type_name'] = $carTypeName[$item['car_type']];
            $item['authentication_name'] = $authenticationName[$item['is_authentication']];
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     * 修改信息
     * @param $saveData
     * @return static
     */
    function editData($id,$saveData){
        return $this->where('id',$id)->update($saveData);
    }



}
